<html>
 <head>
  <meta charset="UTF-8">
 </head>
 <body>
  <h2 data-lake-id="3c4e0a25" id="3c4e0a25"><span data-lake-id="u4ec1ce6f" id="u4ec1ce6f">典型回答</span></h2>
  <p data-lake-id="uaf0ca78e" id="uaf0ca78e"><br></p>
  <p data-lake-id="uf535e9a8" id="uf535e9a8"><strong><span data-lake-id="ub2798d51" id="ub2798d51">网络问题是指在典型的C/S或B/S通信中，客户端或浏览器向服务端发送数或接受数据的时候，因为网络不稳定或接收方异常导致的数据包丢失、无法连接等问题</span></strong><span data-lake-id="ub84f815f" id="ub84f815f">。通常情况下TCP会通过重试的机制确保数据包在一段时间后到达接收方。如果是UDP需要应用自身实现重传机制确保在出现丢包后可重新发送。</span></p>
  <p data-lake-id="ub3abd073" id="ub3abd073"><br></p>
  <blockquote data-lake-id="u4bc8e243" id="u4bc8e243">
   <p data-lake-id="u512fdfcc" id="u512fdfcc"><span data-lake-id="u82454911" id="u82454911">注意本文讨论的内容基于Linux操作系统</span></p>
  </blockquote>
  <p data-lake-id="u67b4b4d1" id="u67b4b4d1"><br></p>
  <p data-lake-id="u4ee1acd7" id="u4ee1acd7"><span data-lake-id="uc323ba5f" id="uc323ba5f">我们可以使用一些基本的命令检测是否出现丢包、目标端口异常：</span></p>
  <p data-lake-id="u1833826b" id="u1833826b"><br></p>
  <ul list="uf4fbdbe8">
   <li fid="u27f9d080" data-lake-id="uc92ebc3f" id="uc92ebc3f"><code data-lake-id="u1172214b" id="u1172214b"><span data-lake-id="uf2dac932" id="uf2dac932">ping</span></code><span data-lake-id="ua62a39d7" id="ua62a39d7"> 检测本机到目标主机是否通畅</span></li>
   <li fid="u27f9d080" data-lake-id="u50c6e990" id="u50c6e990"><code data-lake-id="ua4501336" id="ua4501336"><span data-lake-id="u66e60afc" id="u66e60afc">telnet</span></code><span data-lake-id="uf5c5d06a" id="uf5c5d06a"> 检测本机到目标主机端口是否通畅</span></li>
   <li fid="u27f9d080" data-lake-id="u2ab06623" id="u2ab06623"><code data-lake-id="uda75bb4e" id="uda75bb4e"><span data-lake-id="u41106d8f" id="u41106d8f">traceroute</span></code><span data-lake-id="u29a1079d" id="u29a1079d"> 检测本机到目标主机经过的路由是否通畅</span></li>
  </ul>
  <h3 data-lake-id="RlKco" id="RlKco"><span data-lake-id="u97485737" id="u97485737">ping</span></h3>
  <p data-lake-id="u429ce2d8" id="u429ce2d8"><span data-lake-id="ua7e38c8f" id="ua7e38c8f"><br></span><code data-lake-id="ud4b2694e" id="ud4b2694e"><span data-lake-id="u44bea9e2" id="u44bea9e2">ping</span></code><span data-lake-id="u03890de9" id="u03890de9">命令会发出</span><code data-lake-id="u9b4e2885" id="u9b4e2885"><span data-lake-id="u7ec83119" id="u7ec83119">ICMP</span></code><span data-lake-id="ufde55b2e" id="ufde55b2e">类型的数据包，使用方法：</span></p>
  <p data-lake-id="ub1ad039c" id="ub1ad039c"><br></p>
  <pre lang="java"><code>
ping 目标ip/域名 # 如果不使用-c参数会一直ping，按ctrl+c停止
</code></pre>
  <p data-lake-id="u51afca02" id="u51afca02"><br></p>
  <p data-lake-id="u5a038fcd" id="u5a038fcd"><img src="https://cdn.nlark.com/yuque/0/2023/png/5378072/1701596362941-c6beac84-cf73-4439-a984-7392bd5d34e8.png?x-oss-process=image%2Fwatermark%2Ctype_d3F5LW1pY3JvaGVp%2Csize_16%2Ctext_SmF2YSA4IEd1IFA%3D%2Ccolor_FFFFFF%2Cshadow_50%2Ct_80%2Cg_se%2Cx_10%2Cy_10"></p>
  <p data-lake-id="u74b9c420" id="u74b9c420"><br></p>
  <p data-lake-id="u03b8e64e" id="u03b8e64e"><span data-lake-id="u2b3133c5" id="u2b3133c5">注意：ping的时候是不需要加"http"或者"https"的，直接ping域名。</span></p>
  <p data-lake-id="uf8e72203" id="uf8e72203"><br></p>
  <p data-lake-id="u4c280b3a" id="u4c280b3a"><span data-lake-id="u791e1cbe" id="u791e1cbe">ping存在的问题：</span></p>
  <p data-lake-id="u356f5c00" id="u356f5c00"><br></p>
  <ol list="u2b47b54b">
   <li fid="uccd2d27d" data-lake-id="ufa803d51" id="ufa803d51"><span data-lake-id="u3842e71d" id="u3842e71d">由于ping使用ICMP协议，一些主机防火墙会丢弃ICMP报文，所以会导致ping不同</span></li>
   <li fid="uccd2d27d" data-lake-id="u79838368" id="u79838368"><span data-lake-id="ud016ee89" id="ud016ee89">ping只是探测本机到目标主机的通路，并不探测目标端口是否有效</span></li>
  </ol>
  <p data-lake-id="u0ef6254e" id="u0ef6254e"><br></p>
  <p data-lake-id="ue79d9c80" id="ue79d9c80"><br></p>
  <h3 data-lake-id="Mtuxx" id="Mtuxx"><span data-lake-id="ue336f9eb" id="ue336f9eb">telnet</span></h3>
  <p data-lake-id="u8c93a136" id="u8c93a136"><br></p>
  <p data-lake-id="uaf0fa78c" id="uaf0fa78c"><span data-lake-id="udbbd6aa0" id="udbbd6aa0">telnet 是一个网络协议，同时也是一个使用该协议的命令行工具。它最初被设计用于远程登录到服务器，但由于其安全性问题（主要是信息传输不加密），在现代网络环境中已经较少用于远程登录。然而，telnet 命令仍被广泛用作一种简单的工具来测试网络连接，特别是用于检查指定的IP地址和端口是否可达。</span></p>
  <p data-lake-id="u429017d2" id="u429017d2"><span data-lake-id="u3095b4da" id="u3095b4da">​</span><br></p>
  <pre lang="java"><code>
telnet 目标ip/域名 端口 
</code></pre>
  <p data-lake-id="u80fa8b0f" id="u80fa8b0f"><br></p>
  <p data-lake-id="u2656d0e5" id="u2656d0e5"><img src="https://cdn.nlark.com/yuque/0/2023/png/5378072/1701596696681-66b4063b-7ed4-4234-9291-0ea771e69d1f.png?x-oss-process=image%2Fwatermark%2Ctype_d3F5LW1pY3JvaGVp%2Csize_41%2Ctext_SmF2YSA4IEd1IFA%3D%2Ccolor_FFFFFF%2Cshadow_50%2Ct_80%2Cg_se%2Cx_10%2Cy_10"></p>
  <p data-lake-id="u2aeef63b" id="u2aeef63b"><br></p>
  <p data-lake-id="uf4172803" id="uf4172803"><span data-lake-id="u996ae50a" id="u996ae50a">telnet存在的问题：</span></p>
  <p data-lake-id="ueaffceeb" id="ueaffceeb"><br></p>
  <ol list="ud5c12cfd">
   <li fid="u75a7124f" data-lake-id="ud48d33c0" id="ud48d33c0"><span data-lake-id="uc2443f9d" id="uc2443f9d">无法感知到达目标主机的响应耗时</span></li>
   <li fid="u75a7124f" data-lake-id="u58ccfd40" id="u58ccfd40"><span data-lake-id="u924686b8" id="u924686b8">交互式的体验，不太适合写在shell脚本中</span></li>
   <li fid="u75a7124f" data-lake-id="u78ddd331" id="u78ddd331"><span data-lake-id="ua90be308" id="ua90be308">telnet 不支持加密，发送的数据（包括登录凭据）都是明文。因此，它不适合用于任何涉及敏感信息的场景。</span></li>
  </ol>
  <p data-lake-id="uad6de83c" id="uad6de83c"><span data-lake-id="uc8abd795" id="uc8abd795">​</span><br></p>
  <h3 data-lake-id="xXi6t" id="xXi6t"><span data-lake-id="u048e0673" id="u048e0673">traceroute</span></h3>
  <p data-lake-id="uc78c7d6a" id="uc78c7d6a"><br></p>
  <p data-lake-id="u4b76d2fd" id="u4b76d2fd"><span data-lake-id="u7c1fe20a" id="u7c1fe20a">traceroute 是一个网络诊断工具，用于显示数据包从源主机到目标主机之间经过的路由（路径）。它可以帮助诊断数据包传输中的问题，例如确定网络延迟的位置或查看数据包的路径。</span></p>
  <p data-lake-id="u1fe45530" id="u1fe45530"><br></p>
  <pre lang="java"><code>
traceroute 目标ip/域名 

#traceroute常用参数：
1. -w 秒数 等待远端主机响应的时间，高于这个时间算为超时
2. -i 网卡 使用指定网卡发出数据包
3. -g 网关ip 设置经过哪个网关发出数据包
4. -m 跳数 设置经过路由的最大跳数
</code></pre>
  <p data-lake-id="u28ea685c" id="u28ea685c"><img src="6563f180ab64414ebd07517a.png?x-oss-process=image%2Fwatermark%2Ctype_d3F5LW1pY3JvaGVp%2Csize_9%2Ctext_SmF2YSA4IEd1IFA%3D%2Ccolor_FFFFFF%2Cshadow_50%2Ct_80%2Cg_se%2Cx_10%2Cy_10"><span data-lake-id="u80b94a3c" id="u80b94a3c"><br></span><span data-lake-id="ufbb78394" id="ufbb78394">traceroute主要用于跟踪路由，排查数据包到达目标主机之前产生的问题。</span></p>
  <p data-lake-id="u42c15b3a" id="u42c15b3a"><span data-lake-id="u8b9071c8" id="u8b9071c8">​</span><br></p>
  <p data-lake-id="u168e4928" id="u168e4928"><img src="https://cdn.nlark.com/yuque/0/2023/png/5378072/1701596727184-18f177b6-3b96-4751-8202-077abb59930b.png?x-oss-process=image%2Fwatermark%2Ctype_d3F5LW1pY3JvaGVp%2Csize_59%2Ctext_SmF2YSA4IEd1IFA%3D%2Ccolor_FFFFFF%2Cshadow_50%2Ct_80%2Cg_se%2Cx_10%2Cy_10"></p>
  <p data-lake-id="ub2ee2c6a" id="ub2ee2c6a"><br></p>
  <h1 data-lake-id="GSpA1" id="GSpA1"><span data-lake-id="u001543e6" id="u001543e6">扩展知识</span></h1>
  <p data-lake-id="u8144a4c1" id="u8144a4c1"><br></p>
  <h2 data-lake-id="C9toZ" id="C9toZ"><span data-lake-id="ude9d0327" id="ude9d0327">其它工具</span></h2>
  <p data-lake-id="ucc00fd92" id="ucc00fd92"><br></p>
  <p data-lake-id="ua9539b29" id="ua9539b29"><span data-lake-id="uc200616a" id="uc200616a">curl：一个命令行下的http客户端，可以通过curl打印整个http请求过程的耗时，方法如下：</span></p>
  <p data-lake-id="uc19fe7c4" id="uc19fe7c4"><br></p>
  <pre lang="java"><code>
1. 以下模板保存到一个文件中，如：curl.txt
 \n\nquery time:\n----------\n
time_namelookup:  %{time_namelookup}\n
time_connect:  %{time_connect}\n
time_appconnect:  %{time_appconnect}\n
time_redirect:  %{time_redirect}\n
time_pretransfer:  %{time_pretransfer}\n
time_starttransfer:  %{time_starttransfer}\n
----------\n
time_total:  %{time_total}\n

2. 使用curl 命令发起请求
curl -w '@curl.txt' baidu.com -vvv
</code></pre>
  <p data-lake-id="ueb84fe93" id="ueb84fe93"><br></p>
  <p data-lake-id="u91200690" id="u91200690"><img src="https://cdn.nlark.com/yuque/0/2023/png/5378072/1701596710691-ef59fa8a-8fd5-4c80-8b33-449f0b148975.png?x-oss-process=image%2Fwatermark%2Ctype_d3F5LW1pY3JvaGVp%2Csize_29%2Ctext_SmF2YSA4IEd1IFA%3D%2Ccolor_FFFFFF%2Cshadow_50%2Ct_80%2Cg_se%2Cx_10%2Cy_10"></p>
  <p data-lake-id="u2cae18da" id="u2cae18da"><br></p>
  <p data-lake-id="uddf76dec" id="uddf76dec"><span data-lake-id="u2385ad8e" id="u2385ad8e">各阶段响应耗时解释：</span></p>
  <p data-lake-id="ud6124268" id="ud6124268"><br></p>
  <ul list="u1c832b39">
   <li fid="u3cda36e9" data-lake-id="ua7e72eac" id="ua7e72eac"><span data-lake-id="u7c0f4743" id="u7c0f4743">time_namelookup: 域名解析所需的时间。</span></li>
   <li fid="u3cda36e9" data-lake-id="udd22879a" id="udd22879a"><span data-lake-id="u18905fb2" id="u18905fb2">time_connect: TCP 连接建立所需的时间。</span></li>
   <li fid="u3cda36e9" data-lake-id="udaf3cb73" id="udaf3cb73"><span data-lake-id="u6750d83f" id="u6750d83f">time_appconnect: TLS/SSL &nbsp;握手完成所需的时间。</span></li>
   <li fid="u3cda36e9" data-lake-id="u64d038c4" id="u64d038c4"><span data-lake-id="ub0453d47" id="ub0453d47">time_pretransfer: 从请求开始到响应开始传输的时间。</span></li>
   <li fid="u3cda36e9" data-lake-id="u3b484883" id="u3b484883"><span data-lake-id="u07b0e203" id="u07b0e203">time_redirect: 重定向所需的时间。</span></li>
   <li fid="u3cda36e9" data-lake-id="u333a1f93" id="u333a1f93"><span data-lake-id="u15e61222" id="u15e61222">time_starttransfer: 从请求开始到第一个字节将要传输的时间，包括预传输时间和服务器处理结果所需的时间。</span></li>
   <li fid="u3cda36e9" data-lake-id="u593b3fc8" id="u593b3fc8"><span data-lake-id="u163559bb" id="u163559bb">time_total: 整个请求过程所需的总时间。</span></li>
  </ul>
  <p data-lake-id="ua546d230" id="ua546d230"><br></p>
  <p data-lake-id="u449d5a12" id="u449d5a12"><span data-lake-id="u817f6038" id="u817f6038">tcpping：tcpping是一个第三方的ping工具，可以用来ping某个端口。ping使用IMCP协议，而tcpping是通过TCP协议向给定的端口建立连接。</span><em><span data-lake-id="u9b76801f" id="u9b76801f">注意该工具需要单独安装。</span></em></p>
  <p data-lake-id="udfadda11" id="udfadda11"><br></p>
  <p data-lake-id="ucfcff8d5" id="ucfcff8d5"><img src="https://cdn.nlark.com/yuque/0/2023/png/5378072/1701596763458-71168f3e-3030-4bce-9bba-48ed29aeb77c.png?x-oss-process=image%2Fwatermark%2Ctype_d3F5LW1pY3JvaGVp%2Csize_23%2Ctext_SmF2YSA4IEd1IFA%3D%2Ccolor_FFFFFF%2Cshadow_50%2Ct_80%2Cg_se%2Cx_10%2Cy_10"></p>
  <p data-lake-id="ueeb0223a" id="ueeb0223a"><br></p>
  <pre lang="java"><code>
yum install tcptraceroute -y #安装依赖
wget http://www.vdberg.org/~richard/tcpping #下载tcpping
chmod 755 tcpping #设置可执行权限
./tcpping www.baidu.com 443
</code></pre>
  <p data-lake-id="u5532e8ff" id="u5532e8ff"><img src="65649ffaa1fb851f1400000b.png?x-oss-process=image%2Fwatermark%2Ctype_d3F5LW1pY3JvaGVp%2Csize_9%2Ctext_SmF2YSA4IEd1IFA%3D%2Ccolor_FFFFFF%2Cshadow_50%2Ct_80%2Cg_se%2Cx_10%2Cy_10"></p>
  <p data-lake-id="u9b509fbb" id="u9b509fbb"><span data-lake-id="u72a12233" id="u72a12233">当然如果你不想这么复杂也可以使用</span><code data-lake-id="ue153664b" id="ue153664b"><span data-lake-id="u7e311efc" id="u7e311efc">nc</span></code><span data-lake-id="ud72ae91a" id="ud72ae91a">命令，通常的Linux发行版都会自带这个命令</span></p>
  <p data-lake-id="ue9a1c023" id="ue9a1c023"><br></p>
  <pre lang="java"><code>
nc -zv www.baidu.com 443
</code></pre>
  <p data-lake-id="uc6a25ffc" id="uc6a25ffc"><br></p>
  <p data-lake-id="ued056188" id="ued056188"><img src="https://cdn.nlark.com/yuque/0/2023/png/5378072/1701596752904-d7fd8f52-2c05-40a0-b44e-23065b5563dc.png?x-oss-process=image%2Fwatermark%2Ctype_d3F5LW1pY3JvaGVp%2Csize_16%2Ctext_SmF2YSA4IEd1IFA%3D%2Ccolor_FFFFFF%2Cshadow_50%2Ct_80%2Cg_se%2Cx_10%2Cy_10"></p>
  <p data-lake-id="ube07833e" id="ube07833e"><br></p>
  <p data-lake-id="ubad6bff1" id="ubad6bff1"><span data-lake-id="ue1d3f7a5" id="ue1d3f7a5">tcpdump：tcpdump是很强大的命令行抓包工具，可以抓各种协议的数据包，同时配合wireshark图形化工具来分析网络通信。</span></p>
  <p data-lake-id="u1e166f08" id="u1e166f08"><span data-lake-id="ua9024532" id="ua9024532">​</span><br></p>
  <p data-lake-id="u1106641f" id="u1106641f"><img src="https://cdn.nlark.com/yuque/0/2023/png/5378072/1701596782296-6f37654d-9d61-42a6-88cc-04d1c3e1cc08.png?x-oss-process=image%2Fwatermark%2Ctype_d3F5LW1pY3JvaGVp%2Csize_55%2Ctext_SmF2YSA4IEd1IFA%3D%2Ccolor_FFFFFF%2Cshadow_50%2Ct_80%2Cg_se%2Cx_10%2Cy_10"></p>
  <p data-lake-id="ufbd97750" id="ufbd97750"><span data-lake-id="u564950f1" id="u564950f1"><br>
    tcpdump在一些Linux发行版中可能未安装，需要手动安装。wireshark可以在 </span><a href="https://www.wireshark.org/" target="_blank" data-lake-id="u6aa9d9bd" id="u6aa9d9bd"><span data-lake-id="u8f343498" id="u8f343498">wireshark官网</span></a><span data-lake-id="u24f03254" id="u24f03254"> 进行下载</span></p>
  <p data-lake-id="uf1d57f3c" id="uf1d57f3c"><br></p>
  <pre lang="java"><code>
tcpmdump -i eth0 dst port 443 -w 1.cap #抓取网卡eth0 目标端口为443的数据包保存到1.cap文件中
</code></pre>
  <p data-lake-id="u22b0157a" id="u22b0157a"><br></p>
  <p data-lake-id="u9d2052e0" id="u9d2052e0"><span data-lake-id="u2d691c7d" id="u2d691c7d">通过wireshark打开1.cap进行分析。</span></p>
 </body>
</html>